<?php
function dt_templates_options() {
    $colour = of_get_option( 'misc_colour_schema_select', 'pink' );
	$options = array(
        'dt_home.php'		=> array(
			'front'	=> array(
				'scripts'	=> array(
					'jquery-easing',
                    'dt_slider',
                    'dt_jq_dbtap'
				),
				'action'	=> 'dt_home_action'
			)
		),
        'dt_home_full.php'		=> array(
			'front'	=> array(
				'scripts'	=> array(
					'jquery-easing',
                    'dt_slider',
                    'dt_jq_dbtap'
				),
				'action'	=> 'dt_home_action'
			)
		),
        'dt_blog_slider.php'    => array(
			'front'	=> array(
				'scripts'	=> array(
					'jquery-easing',
                    'dt_slider',
                    'dt_jq_dbtap'
				),
				'action'	=> 'dt_home_action'
			)
		),
        'dt_portfolio.php'    => array(
			'front'	=> array(
				'scripts'	=> array(
					'dt_ajaxing'
				)
			)
		),
        'dt_gallery.php'    => array(
			'front'	=> array(
				'scripts'	=> array(
					'dt_ajaxing'
				)
			)
		),
        'dt_photos.php'    => array(
			'front'	=> array(
				'scripts'	=> array(
					'dt_ajaxing'
				)
			)
		),
		'default'			=> array(
			'front'	=> array(
				'scripts'	=> array(
					'dt_jq_vt',
					'jquery-placeholder',
					'jquery-validationEngine',
					'z.trans',
					'cufon-color-' . $colour,
                    'dt_highslide-full',
                    'dt_shortcodes',
                    'dt_img_loaded',
                    'jquery-aw_showcase'
				),
				'styles'	=> array(
					'dt_html5reset',
					'dt_style',
					'dt_slider',
					'dt_skin-' . $colour,
					'dt_custom',
                    'dt_wp',
                    'dt_highslide',
                    'dt_shortcodes',
                    'dt_old_ie',
                    'dt_img_loaded',
                    'dt_validator'
				),
				'action'	=> 'dt_default_action'
			)
		)
	);
    
    if( of_get_option('misc_like_panel_checkbox', false) ) {
        $options['default']['front']['scripts'][] = 'dt_like';
        $options['default']['front']['styles'][] = 'dt_like';
    }
	
	return $options;
}

function dt_get_template_options( $template_file ) {
	if( !isset($template_file) ) {
		return false;
	}
	
	$options = dt_templates_options();
	
	if( !isset($options[$template_file]) ) {
		return $options['default'];
	}
    
	if( isset($options[$template_file]['front']) ) {
        $opts = $options[$template_file]['front'];
        
        $opts = wp_parse_args( $opts, $options['default']['front'] );
        $opts['scripts'] = wp_parse_args( $opts['scripts'], $options['default']['front']['scripts']);
        $opts['styles'] = wp_parse_args( $opts['styles'], $options['default']['front']['styles']);
        
        $options[$template_file]['front'] = $opts;
    }
    
	return $options[$template_file];
}

function dt_init_scripts( $data = null ) {
	static $scripts = array();
	if( $data && is_array($data) ) {
		$scripts = $data;
		return true;
	}elseif( $data ) {
		return false;
	}
	
	foreach( $scripts as $script ) {
		wp_enqueue_script( $script );
	}
}

function dt_init_stiles( $data = null ) {
	static $styles = array();
	if( $data && is_array($data) ) {
		$styles = $data;
		return true;
	}elseif( $data ) {
		return false;
	}
	
	foreach( $styles as $style ) {
		wp_enqueue_style( $style );
	}
}

//---------------- TEST -------------------------//
function dt_localize_scr_front() {
    $page_type = 'default';
    if( is_page_template('dt_photos.php') ) {
        $page_type = 'photos';
    }elseif( is_page_template('dt_gallery.php') ) {
        $page_type = 'gallery';
    }elseif( is_page_template('dt_portfolio.php') ) {
        $page_type = 'portfolio';
    }

    wp_localize_script(
        'dt_jq_vt',
        'DT_vars',
        array(
            'PageType'  => $page_type
        )
    );
}
//---------------- TEST END ---------------------//

function dt_front_init() {
    $t_name = 'default';
    if( isset($GLOBALS['post']->ID) ) {
        $t_name = get_post_meta( $GLOBALS['post']->ID, '_wp_page_template', true );
    }
    
    $options = dt_get_template_options($t_name);
        
	if( isset($options['front']['styles']) ) {
		dt_init_stiles( $options['front']['styles'] );
	}
	
	if( isset($options['front']['scripts']) ) {
		dt_init_scripts( $options['front']['scripts'] );
	}
	
	if( isset($options['front']['action']) && function_exists($options['front']['action']) ) {
		$options['front']['action']();
	}
}

add_action( 'wp', 'dt_front_init', 99 );

add_action( 'wp_enqueue_scripts', 'dt_init_scripts' );
add_action( 'wp_enqueue_scripts', 'dt_init_stiles' );
add_action( 'wp_enqueue_scripts', 'dt_localize_scr_front' );
